/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rainbow.xml;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.rainbow.annotation.xml.ParentAttr;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 *
 * @author hironaga
 */
class ParentAttrHandler extends XmlHandleComponent{

    private XmlCommonHandler commonHandler = XmlCommonHandler.getInstance();

    ParentAttrHandler() {}

    void writeElement(Annotation annotation, Method getter, Field field, Object obj,
            Document doc, String xpath, int index, Node parentElement, XmlHandleHelper helper)
                                                            throws IOException {

        ParentAttr parentAttrAnnt = (ParentAttr)annotation;
                        String parentAttrName = parentAttrAnnt.attrName();

        //String attrValue;
        Object attrValue;

        try{
            //Method:"getter" invoke: get-property
            attrValue = getter.invoke(obj, new Object[0]);
        } catch (InvocationTargetException ex) {
            return;
        } catch (IllegalAccessException ex) {
            return;
        } catch (IllegalArgumentException ex) {
            return;
        } //try-catch

        //Add Parent Attr
        Node newElement = commonHandler.getLastElement(doc, xpath, index);
        if(newElement == null){
            String elementName = commonHandler.getLastElementNameFromXpath(xpath);
            newElement = commonHandler.appendElement(doc, parentElement, elementName);
        }
        String attrValueStr = commonHandler.getStringVlaue(attrValue);
        commonHandler.setNodeAttr(newElement, parentAttrName, attrValueStr);
        
    }

    void readElement(Annotation annotation, Method setter, Field field,
                                Object obj, Document doc, String xpath,
                                Node parentElement, XmlHandleHelper helper)
                                                            throws IOException {

        ParentAttr parentAttrAnnt = (ParentAttr)annotation;
        String parentAttrName = parentAttrAnnt.attrName();

        Node elementNode = commonHandler.getElementNode(parentElement);

        String attrNodeValue = commonHandler.getNodeAttrValue(elementNode, parentAttrName);

        try{
            //Method:"setter" invoke: set-property
            setter.invoke(obj, new Object[]{
                commonHandler.getPropertyValue(attrNodeValue, field.getType()) } );
        } catch (IllegalArgumentException ex) {
        } catch (InvocationTargetException ex) {
        } finally{
            return;
        }//try-catch
    }

    void overwriteElement(Annotation annotation, Method getter, Field field, Object obj,
            Document doc, String xpath, int index, Node parentElement, XmlHandleHelper helper)
                                                            throws IOException {

        ParentAttr parentAttrAnnt = (ParentAttr)annotation;
        String parentAttrName = parentAttrAnnt.attrName();

        //String attrValue;
        Object attrValue;

        try{
            //Method:"getter" invoke: get-property
            attrValue = getter.invoke(obj, new Object[0]);
        } catch (InvocationTargetException ex) {
            return;
        } catch (IllegalAccessException ex) {
            return;
        } catch (IllegalArgumentException ex) {
            return;
        } //try-catch

        //Exchange Parent Attr
        Node currentElement = commonHandler.getNode(doc, xpath);
        String attrValueStr = commonHandler.getStringVlaue(attrValue);
        commonHandler.setNodeAttr(currentElement, parentAttrName, attrValueStr);
        
    }


}
